<template>
	<view class="Menu">
		<!-- 导航条 :style="{ height: $wanlshop.wanlsys().height + 'px' }"-->
		<view class="cu-custom">
			<view class="cu-bar fixed" style="background: #fff;"
				:style="{height: $wanlshop.wanlsys().height + 'px',paddingTop: $wanlshop.wanlsys().top + 'px',}">
				<view class="action">
					<text class="wlIcon-fanhui1" style="margin-left: 0" @tap="$wanlshop.back(1)"></text>
				</view>
				<view class="content" :style="{ top: $wanlshop.wanlsys().top + 'px' }">
					支付密码设置
				</view>
				<view class="action">
					<text class="wlIcon-gengduo" @tap="showModal('share')"></text>
				</view>
			</view>
		</view>
		<view style="position: fixed;height: auto;  width: auto; top: 0; left: 0; bottom: 0; right: 0;"
			:style="{top: $wanlshop.wanlsys().height + 'px',}">
			<view class="transferBox">
				<view v-if="mobile !=''">
					<view class="formList">
						<h1>手机号</h1>
						<view class="list">
							<u--input border='none' v-model="mobile" placeholder="请输入手机号" :disabled='true'></u--input>
							<view class="code" @click="handleCode">
								{{ counting ? $t('but.againGetCode', {count}) : $t('but.getCode') }}
							</view>
						</view>
					</view>
					<view class="formList">
						<h1>验证码</h1>
						<view class="list">
							<u--input border='none' v-model="captcha" placeholder="请输入验证码"></u--input>
						</view>
					</view>
				</view>
				<view v-else>
					<view class="formList">
						<h1>邮箱</h1>
						<view class="list">
							<u--input border='none' v-model="ems" placeholder="请输入手机号" :disabled='true'></u--input>
							<view class="code" @click="handleCodeEmail">
								{{ counting ? $t('but.againGetCode', {count}) : $t('but.getCode') }}
							</view>
						</view>
					</view>
					<view class="formList">
						<h1>验证码</h1>
						<view class="list">
							<u--input border='none' v-model="captcha" placeholder="请输入验证码"></u--input>
						</view>
					</view>
				</view>
				<view class="formList">
					<h1>支付密码</h1>
					<u-code-input v-model="pwd" :space="0" borderColor="#E6E6E6" size='58' :maxlength="6" :dot='true'
						@finish="finish"></u-code-input>
				</view>
				<view class="formList">
					<h1>确认支付密码</h1>
					<u-code-input v-model="pwd1" :space="0" borderColor="#E6E6E6" size='58' :maxlength="6" :dot='true'
						@finish="finish1"></u-code-input>
				</view>
			</view>
			<view class="uni-btn">
				<button class="uni-btn-v" @click="formSubmit">保存</button>
			</view>

		</view>
	</view>
</template>
<script>
	export default {

		data() {
			return {
				mobile: '',
				ems: '',
				captcha: '',
				count: 60,
				counting: false,
				pwd: '',
				pwd1: '',
			};
		},
		onShow() {
			let info = uni.getStorageSync("wanlshop:user")
			if (info.mobile == '') {
				this.ems = uni.getStorageSync("wanlshop:user").nickname
			} else {
				this.mobile = uni.getStorageSync("wanlshop:user").mobile
			}
		},
		methods: {
			// 发送手机动态码
			handleCode() {
				if (this.counting) return;
				var reg = /^1[3456789]\d{9}$/;
				if (!reg.test(this.mobile)) {
					uni.showToast({
						title: this.$t('otcTransfer.toast.auth.phone'),
						//"请输入正确的手机格式",
						icon: "none",
					});
					return;
				}
				this.getCaptcha();
			},
			// 获取验证码的接口
			getCaptcha() {
				uni.request({
					url: "/sms/send",
					method: "POST",
					data: {
						event: "resetpwd",
						mobile: this.mobile,
					},
					success: (res) => {
						this.$wanlshop.msg(this.$t('otcTransfer.toast.getcode.success')); //"验证码发送成功"
						this.counting = true;
						const timer = setInterval(() => {
							this.count--;
							if (this.count <= 0) {
								clearInterval(timer);
								this.counting = false;
								this.count = 60; // 重置倒计时时长
							}
						}, 1000);
					},
				});
			},
			// 发送邮箱动态码
			handleCodeEmail() {
				if (this.counting) return;
				var reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
				if (!reg.test(this.ems)) {
					uni.showToast({
						title: this.$t('signin.toast.input.error.email'), //"请输入正确的邮箱格式",
						icon: "none",
					});
					return;
				}

				this.getCaptchaEmail();
			},
			// 获取验证码的接口
			getCaptchaEmail() {
				uni.request({
					url: "/ems/send",
					method: "POST",
					data: {
						event: "resetpwd",
						email: this.ems,
					},
					success: (res) => {
						this.$wanlshop.msg(this.$t('signin.toast.success.getCode')); //"验证码发送成功"
						this.counting = true;
						const timer = setInterval(() => {
							this.count--;
							if (this.count <= 0) {
								clearInterval(timer);
								this.counting = false;
								this.count = 60; // 重置倒计时时长
							}
						}, 1000);
					},

				});
			},
			finish(val) {
				this.pwd = val
			},
			finish1(val) {
				this.pwd1 = val
			},
			formSubmit() {

				if (this.captcha == '' || this.captcha == null) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					return
				}
				if (this.pwd == '' || this.pwd == null) {
					uni.showToast({
						title: '请输入支付密码',
						icon: 'none'
					})
					return
				}
				if (this.pwd1 == '' || this.pwd1 == null) {
					uni.showToast({
						title: '请输入确认支付密码',
						icon: 'none'
					})
					return
				}
				if (this.pwd1 !== this.pwd) {
					uni.showToast({
						title: '密码不一致',
						icon: 'none'
					})
					return
				}
				let mobile = ''
				if (this.mobile == '') {
					mobile = this.ems
				} else {
					mobile = this.mobile
				}
				uni.request({
					url: "/user/rezPayzPassword",
					method: "POST",
					data: {
						paypassword: this.pwd,
						mobile: mobile,
						captcha: this.captcha
					},
					success: (res) => {
						if (res.res.code == 1) {
							uni.showToast({
								title: res.res.msg,
								icon: 'none'
							})
							setTimeout(function() {
								uni.reLaunch({
									url: '/pages/newPage/myOtc'
								})
							}, 500)

						}
					},
				});
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: #fff;
	}

	.Menu {

		.transferBox {
			padding: 0 24rpx;

			.formList {
				h1 {
					font-weight: 400;
					font-size: 28rpx;
					color: #333333;
					line-height: 48rpx;
					margin: 30rpx 0;
				}

				.list {
					background: #F8F8F8;
					border-radius: 16rpx;
					padding: 30rpx;
					position: relative;
				}

				/deep/.u-input__content {


					.u-input__content__field-wrapper__field {
						font-weight: 400;
						font-size: 28rpx !important;
						color: #333333 !important;
					}
				}

				.code {
					font-size: 28rpx;
					color: #eb5641;
					position: absolute;
					right: 30rpx;
					top: 32rpx;
				}

			}
		}

		.uni-btn-v {
			width: 702rpx;
			height: 96rpx;
			background: #eb5641;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin-top: 40rpx;
			font-weight: 400;
			font-size: 28rpx;
			color: #ffffff;
			line-height: 96rpx;
			position: fixed;
			left: 24rpx;
			bottom: 80rpx;
		}
	}
</style>